package cloud.zhixinglu.www.spring.boot.jdbc.service.impl;


import cloud.zhixinglu.www.spring.boot.jdbc.entity.TestEntity;
import cloud.zhixinglu.www.spring.boot.jdbc.service.TestService;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  服务层实现。
 *
 * @author WeiXin
 * @since 2023-08-12
 */
@Service
public class TestServiceImpl implements TestService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public TestEntity getTestByOperationId(String operationId) {
        try {
            String sql = "SELECT * FROM test WHERE test.operation_id = ? limit 1";
            // 需要注意，下标以1开始
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TestEntity.class), operationId);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<TestEntity> getTestListByOperationId(String operationId) {
        try {
            String sql = "SELECT * FROM test WHERE test.operation_id = ?";
            // 需要注意，下标以1开始
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TestEntity.class), operationId);
        } catch (EmptyResultDataAccessException e) {
            return new ArrayList<>();
        }
    }

    @Override
    public List<TestEntity> selectListByLimit(Integer limit) {
        try {
            String sql = "SELECT * FROM test limit ?";
            // 需要注意，下标以1开始
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TestEntity.class), limit);
        } catch (EmptyResultDataAccessException e) {
            return new ArrayList<>();
        }
    }



}
